package com.af.security.mgt.role;

import com.af.security.mgt.permisson.Permission;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;

@Entity
@Table(name="sys_role")
public class Role implements Serializable{

	@Id
	@Column(name="role_id")
	private Long id;
	
	private String name;
	
	private String descr;

	private String role;

	@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@JoinTable(name="sys_role_permission",joinColumns = { @JoinColumn(name="role_id")},inverseJoinColumns = @JoinColumn(name="permission_id"))
	private Collection<Permission> permissions = new HashSet<>();

	@Transient
	private Boolean selected;

	public static Role of(Long id, String name, String descr, String roleName){
		Role role= new Role();
		role.setId(id);
		role.setName(name);
		role.setDescr(descr);
		role.setRole(roleName);
		return role;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescr() {
		return descr;
	}

	public void setDescr(String descr) {
		this.descr = descr;
	}
	

	public Boolean isSelected() {
		return selected;
	}

	public void setSelected(Boolean selected) {
		this.selected = selected;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public Collection<Permission> getPermissions() {
		return permissions;
	}

	public void setPermissions(Collection<Permission> permissions) {
		this.permissions = permissions;
	}

	@Override
	public String toString() {
		return "Role{" +
				"id=" + id +
				", name='" + name + '\'' +
				", descr='" + descr + '\'' +
				", role='" + role + '\'' +
//				", permissions=" + permissions +
				", selected=" + selected +
				'}';
	}
}
